package com.example.demo.service.impl;

import com.example.demo.model.WxUser;
import com.example.demo.repository.WxUserRepository;
import com.example.demo.service.WxUserService;
import com.example.demo.util.WXConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxUserServiceImpl implements WxUserService {

    @Autowired
    private WxUserRepository wxUserRepository;
    @Autowired
    private WXConfig wxConfig;

    public WXConfig getWxConfig() {
        return wxConfig;
    }

    @Override
    public long saveOne(WxUser wxUser) {
        List<WxUser> list = wxUserRepository.findByOpenID(wxUser.getOpenID());
        if (!list.isEmpty()) {
            WxUser resUser = wxUserRepository.save(wxUser);
            return resUser.getId();
        } else {
            wxUser.setCreateTime(list.get(0).getCreateTime());
            wxUserRepository.delete(list.get(0));
            wxUserRepository.save(wxUser);
        }
        return 0;
    }

}
